
import {	DEL_MSG,ALL_READ,SET_READ} from './actionTypes'
let defaultState = {
	msgLists:[
		{
			title:"消息1",
			content:"多好玩ID我hi单位hi京东网喜欢此外",
			read: false
		},
		{
			title:"消息2",
			content:"多好玩ID我hi单位hi京东网喜欢此外222",
			read: false
		},
		{
			title:"消息3",
			content:"多好玩ID我hi单位hi33434京东网喜欢此外",
			read: false
		},
		{
			title:"消息444",
			content:"多好玩ID我hi单位hi京fefe东网喜欢此外",
			read: false
		}
	]
}
// 去缓存  判断 缓存有没有值 有赋值 没有不赋值
let reduxState = localStorage.getItem('reduxState');
if(reduxState){
  defaultState = JSON.parse(reduxState)
}

const msgReducer = (state=defaultState,action)=>{
  let newState = JSON.parse(JSON.stringify(state));
	switch (action.type){
		case DEL_MSG: //删除消息
			newState.msgLists.splice(action.index,1)
			break;
		case ALL_READ :  //全部已读
			newState.msgLists.forEach(msg=>{msg.read=true})
			break;
		case SET_READ: //已读
			newState.msgLists[action.index].read = true
			break;
		default:
			break;
	}
  localStorage.setItem('reduxState',JSON.stringify(newState))
  return newState;
}

export default msgReducer